from DrissionPage import ChromiumPage
import pyautogui
from tqdm import tqdm
from bs4 import BeautifulSoup
import pymysql
import time
import re
import pandas as pd

# 设置超时时间为10分钟
timeout = 5 * 60

driver = ChromiumPage()
driver.set.window.max()
driver.get('https://www.douyin.com/search/type%3Dvideo?type=video')
driver.wait(2)


def filter_comment(comment):
    """
    过滤评论中的非法字符
    """
    # 使用正则表达式替换非法字符为空字符串
    filtered_comment = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9.,!?;:()\'"\-_ ]', '', comment)
    return filtered_comment


DF_EMPTY = pd.DataFrame()

video_list = [
"https://www.douyin.com/video/7450776051988319514",
"https://www.douyin.com/video/7450761724816411938",
"https://www.douyin.com/video/7450733065942420799",
"https://www.douyin.com/video/7450691937654295818",
"https://www.douyin.com/video/7450464907142712627",
"https://www.douyin.com/video/7450443195386842408",
"https://www.douyin.com/video/7450425257740995849",
"https://www.douyin.com/video/7450329289137982783",
"https://www.douyin.com/video/7450171583831772467",
"https://www.douyin.com/video/7450104602696895807",
"https://www.douyin.com/video/7450077583657520396",
"https://www.douyin.com/video/7450032345408703798",
"https://www.douyin.com/video/7450046473514814780",
"https://www.douyin.com/video/7449996193138101519",
"https://www.douyin.com/video/7449971456320015667",
"https://www.douyin.com/video/7449729146449644857",
"https://www.douyin.com/video/7449718455420390667",
"https://www.douyin.com/video/7449718433362496807",
"https://www.douyin.com/video/7449718422058945846",
"https://www.douyin.com/video/7449706654666378553",
"https://www.douyin.com/video/7449654823416335670",
"https://www.douyin.com/video/7449641627536100647",
"https://www.douyin.com/video/7449564719154875685",
"https://www.douyin.com/video/7449353531280117050",
"https://www.douyin.com/video/7449331776356502818",
"https://www.douyin.com/video/7449292794184617252",
"https://www.douyin.com/video/7449292790246296895",
"https://www.douyin.com/video/7449222963750014217",
"https://www.douyin.com/video/7449012662010154266",
"https://www.douyin.com/video/7448843520263064866",
"https://www.douyin.com/video/7448585640976993593",
"https://www.douyin.com/video/7448613278810393919",
"https://www.douyin.com/video/7448599043829271867",
"https://www.douyin.com/video/7448584969909226810",
"https://www.douyin.com/video/7448253317781671228",
"https://www.douyin.com/video/7448106663439060284",
"https://www.douyin.com/video/7448081445538516258",
"https://www.douyin.com/video/7447867587758804234",
"https://www.douyin.com/video/7447855386884918567",
"https://www.douyin.com/video/7447851170216807690",
"https://www.douyin.com/video/7447850861193104697",
"https://www.douyin.com/video/7447523632688975161",
"https://www.douyin.com/video/7447461145612422458",
"https://www.douyin.com/video/7447507484429913402",
"https://www.douyin.com/video/7447463637247118601",
"https://www.douyin.com/video/7447423430065966363",
"https://www.douyin.com/video/7447360879089306937",
"https://www.douyin.com/video/7447136573084241210",
"https://www.douyin.com/video/7447136329126710587",
"https://www.douyin.com/video/7447098353378250003",
"https://www.douyin.com/video/7447080429301108007",
"https://www.douyin.com/video/7447065814244592908",
"https://www.douyin.com/video/7447079822444072203",
"https://www.douyin.com/video/7447057399875144970",
"https://www.douyin.com/video/7446926689919143228",
"https://www.douyin.com/video/7446801494487158074",
"https://www.douyin.com/video/7446759459935882506",
"https://www.douyin.com/video/7446430228206849339",
"https://www.douyin.com/video/7446363557307911476",
"https://www.douyin.com/video/7446331381006781755",
"https://www.douyin.com/video/7446289464768449842",
"https://www.douyin.com/video/7446274270579182886",
"https://www.douyin.com/video/7446264924524793114",
"https://www.douyin.com/video/7446050680395042086",
"https://www.douyin.com/video/7446025452545723711",
"https://www.douyin.com/video/7445588439522741558",
"https://www.douyin.com/video/7445973769925037339",
"https://www.douyin.com/video/7445836624916745510",
"https://www.douyin.com/video/7445586028020206908",
"https://www.douyin.com/video/7445574554207997193",
"https://www.douyin.com/video/7445310604052598073",
"https://www.douyin.com/video/7445274631302286643",
"https://www.douyin.com/video/7445161107318213915",
"https://www.douyin.com/video/7444865135090437427",
"https://www.douyin.com/video/7444898285363481913",
"https://www.douyin.com/video/7444863273641446682",
"https://www.douyin.com/video/7444870426569854217",
"https://www.douyin.com/video/7444847953023339786",
"https://www.douyin.com/video/7444834290132618536",
"https://www.douyin.com/video/7444737093185686835",
"https://www.douyin.com/video/7444774245000957196",
"https://www.douyin.com/video/7444567193691491644",
"https://www.douyin.com/video/7444332219734232357",
"https://www.douyin.com/video/7444202618726927667",
"https://www.douyin.com/video/7444158771359763764",
"https://www.douyin.com/video/7444102143335730489",
"https://www.douyin.com/video/7444043624330841344",
"https://www.douyin.com/video/7444020652685692186",
"https://www.douyin.com/video/7443835850124758331",
"https://www.douyin.com/video/7443815788328340786",
"https://www.douyin.com/video/7443786248528940346",
"https://www.douyin.com/video/7443749277664398630",
"https://www.douyin.com/video/7443712967625018650",
"https://www.douyin.com/video/7443711654900092218",
"https://www.douyin.com/video/7443654778871188745",
"https://www.douyin.com/video/7443575342943456538",
"https://www.douyin.com/video/7443273695390469428",
"https://www.douyin.com/video/7443364591779826983",
"https://www.douyin.com/video/7443299080047988028",
"https://www.douyin.com/video/7443275107813936420",
"https://www.douyin.com/video/7455324466671865148",
"https://www.douyin.com/video/7455304792664083749",
"https://www.douyin.com/video/7455300645508173097",
"https://www.douyin.com/video/7455291307724950799",
"https://www.douyin.com/video/7455223039932779802",
"https://www.douyin.com/video/7455268051202723122",
"https://www.douyin.com/video/7455250710913928458",
"https://www.douyin.com/video/7455218934518746404",
"https://www.douyin.com/video/7455201549397118248",
"https://www.douyin.com/video/7455196437937245480",
"https://www.douyin.com/video/7455189841127410979",
"https://www.douyin.com/video/7455146961830710547",
"https://www.douyin.com/video/7455125499535199539",
"https://www.douyin.com/video/7454954360594861372",
"https://www.douyin.com/video/7454895826435935529",
"https://www.douyin.com/video/7454887035426344242",
"https://www.douyin.com/video/7454834404284812580",
"https://www.douyin.com/video/7454514708490571043",
"https://www.douyin.com/video/7454789829860101428"
]
for video_url in video_list:
    print(video_url)
    driver.wait(2)
    driver.get(video_url)
    driver.wait(5)
    try:
        if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_video.png', confidence=0.7):
            print('该视频已下架')
            continue
    except:
        pass
    pyautogui.moveTo(1688, 904)
    start_time = time.time()
    while True:
        if time.time() - start_time > timeout:
            break
        try:
            more_location = pyautogui.locateOnScreen(r'pictures/reply.png', confidence=0.8)
            pyautogui.click(more_location)
            driver.wait(0.8)
        except:
            try:
                more_comment_location = pyautogui.locateOnScreen(r'pictures/more.png', confidence=0.77)
                pyautogui.click(more_comment_location)
                driver.wait(0.4)
            except:
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_more_comment.png',
                                                confidence=0.8):
                        break
                except:
                    pass
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_more_comment2.png',
                                                confidence=0.8):
                        break
                except:
                    pass
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_comment3.png',
                                                confidence=0.8):
                        break
                except:
                    pass
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_comment4.png',
                                                confidence=0.8):
                        break
                except:
                    pass
        pyautogui.scroll(-300)
        driver.wait(0.8)

    comment_bolcks = driver.ele('.Rwb9ssMc comment-mainContent').eles('.UuCzPLbi')
    # 各个评论对,也就是一块块评论块
    for comment_bolck_index, comment_block in enumerate(comment_bolcks):
        html_content_text = comment_block.html
        soup = BeautifulSoup(html_content_text, 'lxml')
        comments = soup.findAll('div', class_='Vrj4Q3zT fiDvPS80')
        # 评论块里的每一行
        comment_history = ''
        for comment_item in comments:
            authors = comment_item.findAll('a', class_='uz1VJwFY')
            # 用户昵称
            author_name = authors[0].get_text(strip=True)
            print(f'作者:{author_name}')
            author_name = filter_comment(author_name)
            # 评论内容
            try:
                comment_text = comment_item.find('span', class_='WFJiGxr7').get_text(strip=True)
            except:
                comment_text = None
                continue
            print(f'评论:{comment_text}')
            # 过滤评论内容
            comment_text = filter_comment(comment_text)

            # 评论时间和地点
            comment_time_space = comment_item.find('div', class_='fJhvAqos').get_text(strip=True)
            comment_time = comment_time_space.split('·')[0]
            try:
                comment_space = comment_time_space.split('·')[1]
            except:
                comment_space = None
            print(f'时间:{comment_time}')
            print(f'地点:{comment_space}')
            # 用户点赞数
            comment_support_number = comment_item.find('p', class_='xZhLomAs').get_text(strip=True)
            if '万' in comment_support_number:
                comment_support_number = int(float(comment_support_number.split('万')[0]) * 10000)
            else:
                comment_support_number = int(comment_support_number)

            print(f'点赞数:{comment_support_number}')
            print(f'问答对编号:{comment_bolck_index}')
            if comment_text == comment_history:
                continue
            else:
                comment_history = comment_text
            dic1 = {
                '视频链接': video_url,
                '作者': author_name,
                '评论': comment_text,
                '时间': comment_time,
                '地点': comment_space,
                '点赞数': comment_support_number,
                '问答对编号': comment_bolck_index
            }
            df1 = pd.DataFrame(dic1, index=[0])
            DF_EMPTY = pd.concat([DF_EMPTY, df1], axis=0)
            DF_EMPTY.to_excel('E:/内部各渠道评论数据留存与分析/指定评论2.xlsx', index=False)
driver.quit()
